package com.java.common.config;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.UrlBasedCorsConfigurationSource;
import org.springframework.web.filter.CorsFilter;
import org.springframework.web.method.support.HandlerMethodArgumentResolver;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.PathMatchConfigurer;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import com.java.common.boot.interceptor.AbstractRepeatSubmitInterceptor;
import com.java.common.boot.interceptor.ApiTokenInterceptor;
import com.java.common.boot.interceptor.PcTokenInterceptor;
import com.java.common.boot.resolver.RequestUserHandlerMethodArgumentResolver;
import com.java.common.constant.Constants;
import com.java.common.constant.SecurityConstants;
import com.java.common.lang.enums.RestPrefixEnum;

/**
 * 通用配置
 * 
 * @author Administrator
 */
@Configuration
public class ResourcesConfig implements WebMvcConfigurer {

    @Autowired
    private AbstractRepeatSubmitInterceptor repeatSubmitInterceptor;

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        /** 本地文件上传路径 */
        registry.addResourceHandler(Constants.RESOURCE_PREFIX + "/**")
                .addResourceLocations("file:" + ProjectConfig.getUploadDir() + "/");
        /** static静态资源访问 **/
        registry.addResourceHandler("/static/**").addResourceLocations("classpath:/static/");
        /** swagger配置 */
        registry.addResourceHandler("doc.html").addResourceLocations("classpath:/META-INF/resources/");
        registry.addResourceHandler("swagger-ui.html").addResourceLocations("classpath:/META-INF/resources/");
        registry.addResourceHandler("/webjars/**").addResourceLocations("classpath:/META-INF/resources/webjars/");
    }

    @Bean
    public ApiTokenInterceptor apiTokenInterceptor() {
        return new ApiTokenInterceptor();
    }

    @Bean
    public PcTokenInterceptor pcTokenInterceptor() {
        return new PcTokenInterceptor();
    }

    /**
     * 自定义拦截规则
     */
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(repeatSubmitInterceptor).addPathPatterns("/**");
        registry.addInterceptor(apiTokenInterceptor()).addPathPatterns(SecurityConstants.API_PATH_PATTERNS);
        registry.addInterceptor(pcTokenInterceptor()).addPathPatterns("/**")
                .excludePathPatterns(SecurityConstants.PC_ANONYMOUS_ANY_ANT_PATTERNS);
    }

    /**
     * 跨域配置
     */
    @Bean
    public CorsFilter corsFilter() {
        UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
        CorsConfiguration config = new CorsConfiguration();
        config.setAllowCredentials(true);
        // 设置访问源地址
        config.addAllowedOriginPattern("*");
        // 设置访问源请求头
        config.addAllowedHeader("*");
        // 设置访问源请求方法
        config.addAllowedMethod("*");
        // 对接口配置跨域设置
        source.registerCorsConfiguration("/**", config);
        return new CorsFilter(source);
    }

    /**
     * 接口统一加前缀
     */
    @Override
    public void configurePathMatch(PathMatchConfigurer configurer) {

        // 控制器路由统一加前缀
        for (RestPrefixEnum restPrefix : RestPrefixEnum.values()) {
            configurer.addPathPrefix(restPrefix.getPathPrefix(), c -> RestPrefixEnum.isAddPrefix(c, restPrefix));
        }

    }

    /**
     * 方法参数解析器
     */
    @Override
    public void addArgumentResolvers(List<HandlerMethodArgumentResolver> argumentResolvers) {
        argumentResolvers.add(new RequestUserHandlerMethodArgumentResolver());
    }

}